package com.itheima.service.impl;

import cn.itheima.health.resources.DateUtils;
import cn.itheima.health.service.ReportService;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.SetmealMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberMapper memberMapper;


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public Map queryMemberByDate(String beginDate, String endDate) {
        try {
            String format = "yyyy-MM";
            Map map = new HashMap();
            List<String> monthBetween = DateUtils.getMonthBetween(beginDate, endDate, format);
            List<String> yearAndMonthList = new ArrayList<>();
            List<Integer> memberNumbersList = new ArrayList<>();
            System.out.println("monthBetween = " + monthBetween);
            for (String s : monthBetween) {
                yearAndMonthList.add(s);
                String beginDates = s + "-1";
                String endDates = s + "-31";
                int count = this.memberMapper.countsMembersByPerMonth(beginDates,endDates);
                memberNumbersList.add(count);
            }
            map.put("yearAndMonth",yearAndMonthList);
            map.put("memberNumbers",memberNumbersList);
            return map;


        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    public Map getMemberReport() {

        Map map = new HashMap();


        List<String> yearsandMonthsList = new ArrayList<String>();
        List<Integer> memberNumbersList = new ArrayList<Integer>();

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -12);

        for (int i = 1; i <= 12; i++) {
            Date time = calendar.getTime(); //  2019-8

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            String format = simpleDateFormat.format(time);//  2019-8

            String beginDate = format + "-1";
            String endDate = format + "-31";

            //  Long  mnum =  mapp.find(xx,xx);

            //System.out.println(format);
            yearsandMonthsList.add(format); //   存储过去一年年月

            //  过去每个月的会员数量统计
            int num = memberMapper.countsMembersByPerMonth(beginDate, endDate);
            memberNumbersList.add(num);
            calendar.add(Calendar.MONTH, 1);
        }


        map.put("yearAndMonth", yearsandMonthsList);
        map.put("memberNumbers", memberNumbersList);
        return map;
    }

    @Override
    public List<Map> getSetmealReport() {

        List<Map> listMap = orderMapper.countSetMealCountsAndNames();

        return listMap;
    }

    //用户年龄段占比 饼图
    @Override
    public List<Map> getMemberPorpertion() {

        List<Map> map = memberMapper.getMemberPorpertion();
        return map;
    }
    //用户性别占比 饼图
    @Override
    public List<Map> getMemberPorpertionBySex() {
        List<Map> list = memberMapper.getMemberPorpertionBySex();
        return list;
    }
}
